library(tidyverse)
## ── Attaching core tidyverse packages ──────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(p8105.datasets)

Load data

data(instacart)

Bar Chart

Filter data

instacart = 
  instacart |> 
  select (department, aisle, order_hour_of_day, order_number, days_since_prior_order)

Plot bar chart

instacart |> 
  count(department) |> 
  filter(department != "missing") |> 
  mutate(department = fct_reorder(department, n)) |> 
  plot_ly (x = ~department, y = ~n, color = ~department,
         type = "bar", colors = "viridis")

Box Plot

instacart |>
  filter(department != "missing") |> 
  mutate(department = fct_reorder(department, order_hour_of_day)) |>
  plot_ly(x = ~department, y = ~order_hour_of_day, color = ~department,
          type = "box", colors = "viridis")

Scatter Plot

instacart_avg = 
  instacart |>
  group_by(order_number) |>
  summarise(avg_days_since_prior_order = mean(days_since_prior_order, na.rm = TRUE))
instacart_avg |>
  plot_ly(x = ~order_number, y = ~avg_days_since_prior_order, color = ~order_number,
      type = "scatter", mode = "markers", colors = "viridis")